package com.gmail.dmitryustimov.ms.java7.chain_of_responsibility.mod.mod_multy_type_visit.testperf;

import com.gmail.dmitryustimov.ms.java7.chain_of_responsibility.mod.mod_multy_type_visit.demo.handlers1_100_requests10.*;
import com.gmail.dmitryustimov.ms.java7.patternslib.chain_of_responsibility.singleevent.Handler;

public class TestPerfHandlers1_100Request10All {

    private static final int NUM_HANDLES = 100000;
    private static final int NUM_WARM_UP = 3;
    private static final int NUM_TESTS = 10;

    private static final int NUM_HANDLERS = 100;

    private static final String MESSAGE = "chain_of_responsibility_mod - visit, 1-100 handlers, 10 request types, handleChain All, took: ";

    public static void main(String[] args) {

        //warm up
        for (int i = 0; i < NUM_WARM_UP; i++) {
            test();
        }

        //tests
        long time = 0;
        for (int j = 0; j < NUM_TESTS; j++) {
            time += test();
        }
        System.out.println("final - " + MESSAGE + time/NUM_TESTS + "ns (" + (time/NUM_TESTS)/1000000 + "ms)");
    }

    private static long test() {
        Handler<Request> h1 = new CocreteHandler1<>("CocreteHandler1", 10);
        Handler<Request> beforeNext = h1;

        for (int i = 1; i < NUM_HANDLERS; i++) {
            Handler<Request> next = new CocreteHandler1<>("CocreteHandler" + (i + 1), 10);
            beforeNext.setNext(next);
            beforeNext = next;
        }

        Request request1 = new Request1("Request1", 5);
        Request request2 = new Request2("Request2", 5);
        Request request3 = new Request3("Request3", 5);
        Request request4 = new Request4("Request4", 5);
        Request request5 = new Request5("Request5", 5);
        Request request6 = new Request6("Request6", 5);
        Request request7 = new Request7("Request7", 5);
        Request request8 = new Request8("Request8", 5);
        Request request9 = new Request9("Request9", 5);
        Request request10 = new Request10("Request10", 5);

        long startTime = System.nanoTime();

        for (long j = 0; j < NUM_HANDLES; j++) {
            h1.handleChain(request1);
            h1.handleChain(request2);
            h1.handleChain(request3);
            h1.handleChain(request4);
            h1.handleChain(request5);
            h1.handleChain(request6);
            h1.handleChain(request7);
            h1.handleChain(request8);
            h1.handleChain(request9);
            h1.handleChain(request10);
        }

        long estimatedTime = System.nanoTime() - startTime;
        System.out.println(MESSAGE + estimatedTime + "ns");
        return estimatedTime;
    }
}
